ModEditor.GUI.ListBox = {}

function ModEditor.GUI.ListBox:DrawEverything(x,y)
	set_color(0,0,0,1)
	for i=self.Top,(self.Height-math.fmod(self.Height,20))/20+self.Top-1 do
		if i <= #self.Items then
			if self.Items[i][2] then
				set_color(0,0,1,1)
				draw_quad(x,y+(i-self.Top)*20,self.Width,20)
				set_color(0,0,0,1)
			end
			draw_text(self.Items[i][1],x,y+(i-self.Top)*20)
		end
	end
end
function ModEditor.GUI.ListBox:OnKeyDown(code)
	if #self.Items == 0 then
		return 0
	end
	if code == 273 then
		if self.SelectedItem == 0 then
			self.SelectedItem = 1
		end
		for i = 1,#self.Items do
			self.Items[i][2] = false
		end
		if self.SelectedItem > 1 then
			self.SelectedItem = self.SelectedItem - 1
			if self.SelectedItem < self.Top then
				self.Top = self.Top - 1
			end
		end
		self.Items[self.SelectedItem][2] = true
		if self.SelectedItem < self.Top then
			self.Top = self.SelectedItem
		elseif self.SelectedItem > (self.Height-math.fmod(self.Height,20))/20+self.Top-1 then
			self.Top = self.SelectedItem-(self.Height-math.fmod(self.Height,20))/20+1
		end
		return 1
	elseif code == 274 then
		if self.SelectedItem == 0 then
			self.SelectedItem = 1
		end
		for i = 1,#self.Items do
			self.Items[i][2] = false
		end
		if self.SelectedItem < #self.Items then
			self.SelectedItem = self.SelectedItem + 1 
			if self.SelectedItem >= (self.Height-math.fmod(self.Height,20))/20+self.Top then
				self.Top = self.Top + 1
			end
		end
		self.Items[self.SelectedItem][2] = true
		if self.SelectedItem < self.Top then
			self.Top = self.SelectedItem
		elseif self.SelectedItem > (self.Height-math.fmod(self.Height,20))/20+self.Top-1 then
			self.Top = self.SelectedItem-(self.Height-math.fmod(self.Height,20))/20+1
		end
		return 1
	end
	return 0
end
function ModEditor.GUI.ListBox:OnKeyUp(code)
	return 0
end
function ModEditor.GUI.ListBox:Activate()
	ModEditor.Input.KeyHook = self
	self.Activated = true
end
function ModEditor.GUI.ListBox:Deactivate()
	ModEditor.Input.KeyHook = nil
	self.Activated = false
end
function ModEditor.GUI.ListBox:MouseScrollDown()
	if self.Top < #self.Items then
		self.Top = self.Top + 1
	end
end
function ModEditor.GUI.ListBox:MouseScrollUp()
	if self.Top > 1 then
		self.Top = self.Top - 1	
	end
end
function ModEditor.GUI.ListBox:MouseDown(x,y)
	local id = (y-math.fmod(y,20))/20+self.Top
	if id <= #self.Items then
		self.SelectedItem = id
		if (ModEditor.Input.LeftCtrl or ModEditor.Input.RightCtrl) and self.MultiSelection then
			self.Items[id][2] = not self.Items[id][2]
		else
			for i = 1,#self.Items do
				self.Items[i][2] = false
			end
			self.Items[id][2] = true
		end
	end
end
function ModEditor.GUI.ListBox:Insert(newitems)
	for i=1,#newitems do
		table.insert(self.Items,{newitems[i],false})
	end
end
function ModEditor.GUI.ListBox:New(parent)
	local obj = ModEditor.GUI.BasicControl:New(parent)
	obj.Activate = self.Activate
	obj.Deactivate = self.Deactivate
	obj.MouseDown = self.MouseDown
	obj.OnKeyDown = self.OnKeyDown
	obj.OnKeyUp = self.OnKeyUp
	obj.DrawEverything = self.DrawEverything
	obj.Insert = self.Insert
	obj.Items = {}
	obj.SelectedItem = 0
	obj.Activated = false
	obj.MultiSelection = false
	obj.Top = 1
	return obj
end
